package netective.gui.charts;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import netective.api.analyzer.PacketDirection;
import netective.api.analyzer.PacketType;
import netective.api.analyzer.PredictionType;
import netective.dao.DatabaseHelper;

public class TimeChartFactory 
{

	public static TimeSeriesChart classesOfPackets(ChartGrain g, PredictionType p, ArrayList<PacketType> types)
	{
		DatabaseHelper dh = new DatabaseHelper();
			ResultSet resultSet[] = new ResultSet[types.size()];
			String legend[] = new String[types.size()];
			for (int i = 0; i < resultSet.length; i++) {
				resultSet[i] = dh.getPacketsCounts("classesOfPackets", types.get(i).toString(), g);
				legend[i] = types.get(i).toString();
			}
			
			return new TimeSeriesChart(null,"Packets by Type", resultSet,legend, g, p);
	}
	
	public static TimeSeriesChart dirrectionOfPackets(ChartGrain g, PredictionType p, String protocols)
	{
		DatabaseHelper dh = new DatabaseHelper();
		ResultSet resultSet[] = new ResultSet[2];
		resultSet[0]=dh.getPacketsCountsFromProtocols("dirrectionOfPackets",
				PacketDirection.In.toString(), protocols, g);
		resultSet[1]=dh.getPacketsCountsFromProtocols("dirrectionOfPackets",
				PacketDirection.Out.toString(), protocols, g);
		String[] legend={"In","Out"};
		return new TimeSeriesChart(null,"In/Out Packets", resultSet,legend,g,p);
	}

	public static TimeSeriesChart downloadUpload(ChartGrain g, PredictionType p, String protocols)
	{
		DatabaseHelper dh = new DatabaseHelper();
		ResultSet resultSet[] = new ResultSet[2];
		resultSet[0]=dh.getPacketsCountsFromProtocols("downloadUpload",PacketDirection.In.toString(), protocols, g);
		resultSet[1]=dh.getPacketsCountsFromProtocols("downloadUpload",PacketDirection.Out.toString(),protocols, g);
		String[] legend={"In","Out"};
		return new TimeSeriesChart(null,"Download/Upload", resultSet,legend,"Bytes",g,p);
	}
	
	public static TimeSeriesChart downloadUploadAverage(ChartGrain g, PredictionType p, String protocols)
	{
		DatabaseHelper dh = new DatabaseHelper();
		ResultSet resultSet[] = new ResultSet[2];
		resultSet[0]=dh.getPacketsCountsFromProtocols("downloadUploadAvg",PacketDirection.In.toString(), protocols, g);
		resultSet[1]=dh.getPacketsCountsFromProtocols("downloadUploadAvg",PacketDirection.Out.toString(),protocols, g);
		String[] legend={"In","Out"};
		return new TimeSeriesChart(null,"Average Packet Size", resultSet,legend,"Bytes",g,p);
	}

	public static TimeSeriesChart outPort(ChartGrain g, PredictionType p, int top, String protocols)
	{
		return myPort(g,p,top,protocols,"outPort", "Output Ports");
	}
	
	public static TimeSeriesChart inPort(ChartGrain g, PredictionType p, int top, String protocols)
	{
		return myPort(g,p,top,protocols,"inPort", "Input Ports");
	}	
	
	
	
	private static TimeSeriesChart myPort(ChartGrain g, PredictionType p, int top, String protocols, String dir, String title)
	{
		DatabaseHelper dh = new DatabaseHelper();
		ResultSet ports=dh.getTopN(dir, top, protocols);
		ResultSet resultSet[] = new ResultSet[top];
		String[] legend = new String[top];
		int licz=0;
		try {		
		if (ports != null) 
		{
			while (ports.next()) 
			{

				resultSet[licz]=dh.getPacketsCounts(dir, ports.getString(1), g);
				legend[licz++]=ports.getString(1);

			}
		}
		} catch (SQLException e) {

			e.printStackTrace();
		}
		return new TimeSeriesChart(null,title, resultSet,legend,g,p);
	}	
	
	public static TimeSeriesChart otherPort(ChartGrain g, PredictionType p, int top, String protocols)
	{
		DatabaseHelper dh = new DatabaseHelper();
		ResultSet ports=dh.getTopN("otherPort", top, protocols);
		ResultSet resultSet[] = new ResultSet[top];
		String[] legend = new String[top];
		int licz=0;
		try {		
		if (ports != null) 
		{
			while (ports.next()) 
			{

				resultSet[licz]=dh.getPacketsCounts("otherPort", ports.getString(1), g);
				legend[licz++]=ports.getString(1);

			}
		}
		} catch (SQLException e) {

			e.printStackTrace();
		}
		return new TimeSeriesChart(null,"Destination Ports", resultSet,legend,g,p);
	}		

	public static TimeSeriesChart otherAdresses(ChartGrain g, PredictionType p, int top, String protocols)
	{
		DatabaseHelper dh = new DatabaseHelper();
		ResultSet ports=dh.getTopN("otherAdresses", top, protocols);
		ResultSet resultSet[] = new ResultSet[top];
		String[] legend = new String[top];
		int licz=0;
		try {		
		if (ports != null) 
		{
			while (ports.next()) 
			{

				resultSet[licz]=dh.getPacketsCounts("otherAdresses", ports.getString(1), g);
				legend[licz++]=ports.getString(1);

			}
		}
		} catch (SQLException e) {

			e.printStackTrace();
		}
		return new TimeSeriesChart(null,"Destination Addresses", resultSet,legend,g,p);
	}	
	
}
